package com.bw.ppjokeplay.commen.mvvm.viewmodel

import android.os.Bundle
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import com.bw.ppjokeplay.commen.entity.BaseEntity
import com.bw.ppjokeplay.commen.field.UIField
import com.bw.ppjokeplay.commen.mvvm.model.BaseModel

import io.reactivex.Observer
import io.reactivex.disposables.CompositeDisposable
import io.reactivex.disposables.Disposable
import java.lang.reflect.ParameterizedType

/**
 * @author Lishaohe
 * @description:
 * @date :2021/11/9 0009 下午 6:15
 *
 */
abstract class BaseViewModel<M : BaseModel> :ViewModel(),IViewModel,Observer<BaseEntity> {
    val m : M
    lateinit var compositeDisposable : CompositeDisposable
    var uiChangeListener: UIChangeListener

 
    //初始化model对象
    init {
        // BaseViewModel::class->kclass->kotlin
        // BaseViewModel::class.java->Class->Java
        //反射获取范型中BaseModel具体类型
        val parameterizedType = this.javaClass.genericSuperclass as ParameterizedType
        //获取全部范型类型数组
        val types = parameterizedType.actualTypeArguments
        val clazz : Class<*> = types[0] as Class<*>
        m = clazz.newInstance() as M
        uiChangeListener = UIChangeListener()
    }

    override fun create() {
        compositeDisposable = CompositeDisposable()
        initdata()
    }

    override fun destrory() {
        compositeDisposable.dispose()
        compositeDisposable.clear()
    }

    fun getUIChange():UIChangeListener{
        return uiChangeListener
    }

    abstract fun initdata()

    //接口请求返回值
    abstract fun result(t: BaseEntity)

    override fun onSubscribe(d: Disposable) {
        compositeDisposable.add(d)
        showDialog()
    }

    override fun onNext(t: BaseEntity) {
        result(t)
    }

    override fun onError(e: Throwable) {
        showMsg(e.message.toString())
    }

    override fun onComplete() {
        hideDialog()
    }

    fun showDialog(){
        getUIChange().setshowDialog(""+System.currentTimeMillis())
    }

    fun hideDialog(){
        getUIChange().sethideDialog(""+System.currentTimeMillis())
    }

    fun showMsg(msg : String){
        getUIChange().setshowMsg(msg)
    }

    fun startPage(clazz : Class<*>,bundle: Bundle){
        val map : HashMap<String,Any> = HashMap<String,Any>()
        map.put(UIField.PAGECLAZZ, clazz)
        if (bundle!=null)
            map.put(UIField.PAGEBUNDLE,bundle)
        getUIChange().startPage(map)
    }

    fun finishPage(){
        getUIChange().finishPage(""+System.currentTimeMillis())
    }

    class UIChangeListener{
        var showDialogLiveData : MutableLiveData<String> ?= null
            get() {
                if (field == null)
                    field = MutableLiveData<String>()
                return field
            }
        var hideDialogLiveData : MutableLiveData<String> ?= null
            get() {
                if (field == null)
                    field = MutableLiveData<String>()
                return field
            }

        var showMsgLiveData : MutableLiveData<String> ?= null
            get() {
                if (field == null)
                    field = MutableLiveData<String>()
                return field
            }

        var startPageLiveData : MutableLiveData<HashMap<String,*>> ?= null
            get() {
                if (field == null)
                    field = MutableLiveData<HashMap<String,*>>()
                return field
            }

        var finishPageLiveData : MutableLiveData<String> ?= null
            get() {
                if (field == null)
                    field = MutableLiveData<String>()
                return field
            }

        fun setshowDialog(str : String){
            showDialogLiveData!!.value = str
        }

        fun sethideDialog(str : String){
            hideDialogLiveData!!.value = str
        }
        fun setshowMsg(str : String){
            showMsgLiveData!!.value = str
        }
        fun finishPage(str : String){
            finishPageLiveData!!.value = str
        }

        fun startPage(map : HashMap<String,*>){
            startPageLiveData!!.value = map
        }


    }
}